function [x,y,z,m,status] = rslice_get_grid ( gdata )
% RSLICE_GET_GRID:  gets x, y, z, and mask grid for a particular variable
%
% PARAMETERS
% Input:
%     gdata:
%         Global structure of rslice data.
% Output:
%     x, y, z, m:
%         X coords, y coords, bathymetry, and mask
%     status:  
%         -1 if the routine fails.  
%         -2 if the named variable is inappropriate
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% $Name: rslice-0_9_2 $
% $Id: rslice_get_grid.m,v 1.4 2005/06/28 20:34:45 jevans Exp $
% AUTHOR:  johnevans@acm.org
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

x = [];
y = [];
z = [];
status = -1;

x = gdata.rslice_grid.x.data;
y = gdata.rslice_grid.y.data;
h = gdata.rslice_grid.roms_grid.h;


%
% Is it a rho, u, or v variable?
is_u_variable = 0;
is_v_variable = 0;
is_rho_variable = 0;
ndims = length(gdata.varinfo.Dimension);
for j = 1:ndims
	switch ( gdata.varinfo.Dimension{j} )
	case 'eta_u'
		is_u_variable = 1;
	case 'eta_v'
		is_v_variable = 1;
	case 'eta_rho'
		is_rho_variable = 1;
	end
end

[r,c] = size(x);
if is_rho_variable
	x = x;
	y = y;
	z = h;
	m = gdata.rslice_grid.roms_grid.mask_rho;
elseif is_u_variable
	x = ( x(:,1:c-1) + x(:,2:c) ) / 2 ;
	y = ( y(:,1:c-1) + y(:,2:c) ) / 2 ;
	z = ( h(:,1:c-1) + h(:,2:c) ) / 2 ;
	m = gdata.rslice_grid.roms_grid.mask_u;
elseif is_v_variable
	x = ( x(1:r-1,:) + x(2:r,:) ) / 2 ;
	y = ( y(1:r-1,:) + y(2:r,:) ) / 2 ;
	z = ( h(1:r-1,:) + h(2:r,:) ) / 2 ;
	m = gdata.rslice_grid.roms_grid.mask_v;
else
	msg = sprintf ( '%s:  could not figure whether %s, was a rho, u, or v variable', mfilename, gdata.varname ) ;
	rslice_error ( msg );
end

status = 0;

return
